#include "printercontrol.h"

PrinterControl* PrinterControl::m_instance = nullptr;
QMutex PrinterControl::m_mutex;

PrinterControl *PrinterControl::getInstance()
{
    if(!m_instance)
    {
        QMutexLocker locker(&m_mutex);
        if(!m_instance)
        {
            m_instance = new PrinterControl();
        }
    }
    return m_instance;
}

void PrinterControl::set_Connect_Parameters(QString port,
                                            QString baudRate,
                                            QString dataBit,
                                            QString parity,
                                            QString stopBit)
{
    QSerialPort::BaudRate set_BaudRate;
    QSerialPort::DataBits set_DataBit;
    QSerialPort::Parity   set_ParityBit;
    QSerialPort::StopBits set_StopBit;

    // 设置波特率
    if("115200" == baudRate)        set_BaudRate = QSerialPort::Baud115200;
    else if("57600" == baudRate)    set_BaudRate = QSerialPort::Baud57600;
    else if("38400" == baudRate)    set_BaudRate = QSerialPort::Baud38400;
    else if("19200" == baudRate)    set_BaudRate = QSerialPort::Baud19200;
    else if("9600" == baudRate)     set_BaudRate = QSerialPort::Baud9600;
    else if("4800" == baudRate)     set_BaudRate = QSerialPort::Baud4800;
    else if("2400" == baudRate)     set_BaudRate = QSerialPort::Baud2400;
    else                            set_BaudRate = QSerialPort::Baud1200;

    // 设置数据位
    if("8" == dataBit)              set_DataBit = QSerialPort::Data8;
    else if("7" == dataBit)         set_DataBit = QSerialPort::Data7;
    else if("6" == dataBit)         set_DataBit = QSerialPort::Data6;
    else                            set_DataBit = QSerialPort::Data5;

    // 设置校验位
    if("None" == parity || "N" == parity)         set_ParityBit = QSerialPort::NoParity;
    else if("Even" == parity || "E" == parity)    set_ParityBit = QSerialPort::EvenParity;
    else if("Odd" == parity || "O" == parity)     set_ParityBit = QSerialPort::OddParity;
    else if("Mark" == parity || "M" == parity)    set_ParityBit = QSerialPort::MarkParity;
    else                            set_ParityBit = QSerialPort::SpaceParity;

    // 设置停止位
    if("1" == stopBit)              set_StopBit = QSerialPort::OneStop;
    else if("1.5" == stopBit)       set_StopBit = QSerialPort::OneAndHalfStop;
    else                            set_StopBit = QSerialPort::TwoStop;

    // 应用串口设置
    m_Printer->setPortName(port);
    m_Printer->setBaudRate(set_BaudRate);
    m_Printer->setDataBits(set_DataBit);
    m_Printer->setParity(set_ParityBit);
    m_Printer->setStopBits(set_StopBit);
    // 设置流控制为无（默认值，显式设置更清晰）
    m_Printer->setFlowControl(QSerialPort::NoFlowControl);
}

void PrinterControl::connet_To_Device()
{
    if (m_Printer->isOpen()) {
        m_Printer->close(); // 先关闭已打开的连接
    }

    if(m_Printer->open(QIODevice::ReadWrite)) {
        qDebug() << "Cpp: 打印机 连接成功，端口：" << m_Printer->portName();
    } else {
        qDebug() << "Cpp: 打印机 连接失败，错误：" << m_Printer->errorString();
    }
}

void PrinterControl::print_Active_WeiHuang(QString Name,
                                           QString Process1,
                                           QString Process2,
                                           QString F_UpperKnife01Ass,
                                           QString F_UpperKnife02Ass,
                                           QString B_UpperKnife01Ass,
                                           QString B_UpperKnife02Ass,
                                           QString F_UpperKnife01,
                                           QString F_UpperKnife02,
                                           QString B_UpperKnife01,
                                           QString B_UpperKnife02,
                                           QString F_LowerKnife01,
                                           QString F_LowerKnife02,
                                           QString B_LowerKnife01,
                                           QString B_LowerKnife02,
                                           QString Roller)
{
    QByteArray printResult;

    QString printWordSet = QString::fromLocal8Bit("1B401B6330021B4C1B54001B57000000008001640118");
    QString printMethod = QString::fromLocal8Bit("0C");

    QString printWord01Set = QString::fromLocal8Bit("1B2400001D240000");
    // QString printWord02Set = QString::fromLocal8Bit("1B2400001D241E00");
    // QString printWord03Set = QString::fromLocal8Bit("1B2400001D243C00");
    // QString printWord04Set = QString::fromLocal8Bit("1B2400001D245A00");
    // QString printWord05Set = QString::fromLocal8Bit("1B2400001D247800");
    // QString printWord06Set = QString::fromLocal8Bit("1B2400001D249600");
    // QString printWord07Set = QString::fromLocal8Bit("1B2400001D24B400");
    // QString printWord08Set = QString::fromLocal8Bit("1B2400001D24D200");

    QString recvQmlLength = QString("名称:%1\n").arg(Name);
    // QString recvQmlHeight = QString("高度:%1\n").arg(MaterialHeight);
    // QString recvQmlLeftAngle = QString("左角度:%1\n").arg(LeftAngle);
    // QString recvQmlRightAngle = QString("右角度:%1\n").arg(RightAngle);
    // QString recvQmlCustomName = QString("%1\n").arg(MaterialName);
    // QString recvQmlMaterialName = QString("%1\n").arg(Customer);

    QString handleLength = recvQmlLength.toLocal8Bit().toHex();
    // QString handleHeight = recvQmlHeight.toLocal8Bit().toHex();
    // QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    // QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    // QString costomTitle = QString("客户名称:").toLocal8Bit().toHex();
    // QString materialTitle = QString("型材名称:").toLocal8Bit().toHex();
    // QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    // QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    QString recvParameter = QString("%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15")
                                .arg(Process1)
                                .arg(Process2)
                                .arg(F_UpperKnife01Ass)
                                .arg(F_UpperKnife02Ass)
                                .arg(B_UpperKnife01Ass)
                                .arg(B_UpperKnife02Ass)
                                .arg(F_UpperKnife01)
                                .arg(F_UpperKnife02)
                                .arg(B_UpperKnife01)
                                .arg(B_UpperKnife02)
                                .arg(F_LowerKnife01)
                                .arg(F_LowerKnife02)
                                .arg(B_LowerKnife01)
                                .arg(B_LowerKnife02)
                                .arg(Roller);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength,2,16,QLatin1Char('0'));
    QString printerQRCodeSet = QString::fromLocal8Bit("1B24C8001D2400001B5A000005%1%2").arg(QR_ParameterLength).arg("00");

    printResult = printWordSet.toUtf8() +
                  printWord01Set.toUtf8() + handleLength.toUtf8() +
                  // printWord02Set.toUtf8() + handleHeight.toUtf8() +
                  // printWord03Set.toUtf8() + handleLeftAngle.toUtf8() +
                  // printWord04Set.toUtf8() + handleRightAngle.toUtf8() +
                  // printWord05Set.toUtf8() + costomTitle.toUtf8() +
                  // printWord06Set.toUtf8() + handleCustomName.toUtf8() +
                  // printWord07Set.toUtf8() + materialTitle.toUtf8() +
                  // printWord08Set.toUtf8() + handleMaterialName.toUtf8() +
                  printerQRCodeSet.toUtf8() + handleParameter.toUtf8() +
                  printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if(m_Printer->write(sendMasage))
    {
        qDebug()<<"炜煌打印机 - 发送成功";
    }
    else
    {
        qDebug()<<"炜煌打印机 - 发送失败";
    }
}

PrinterControl::PrinterControl(QObject *parent)
    : QObject{parent}
{
    m_Printer = new QSerialPort();
}

PrinterControl::~PrinterControl()
{

}


